<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-11-15 16:03:03
 * @Last Modified time: 2022-12-07 19:22:36
 */
namespace app\sys\controller\system;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\system\ConfigService;
use app\common\services\system\ConfigTabService;
/**
 * 配置项
 * Class Config 
 * @package app\sys\controller\system
 */
class Config extends Base
{
    /**
     * Config constructor.
     * @param App $app
     */
    public function __construct(App $app,ConfigService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 配置项列表
     * @method(GET)
     */
    public function list()
    {
        $data = $this->request->getMore([
            ['status', ''],
            ['config_tab_id', -1],
        ]);
        $where['config_tab_id'] = ConfigTabService::instance()->getConfigTabChildIdsByTabId($data['config_tab_id']);
        $this->success($this->service->getConfigList($where));
    }

    /**
     * 配置项详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        $this->success($this->service->getConfigDetail($this->request->get('id')));
    }

    /**
     * 添加配置项
     * @method(POST)
     */
    public function store()
    {
        $data = $this->request->postMore([
           	['config_name', ''],
            ['type', ''],
            ['input_type', 'input'],
            ['config_tab_id', 0],
            ['parameter', ''],
            ['upload_type', 1],
            ['required', ''],
            ['value', ''],
            ['name_field', ''],
            ['desc', ''],
            ['sort', 0],
            ['status', 0]
        ]);
        if (!$data['config_name']) $this->error('请输入配置名称');
        if (!$data['name_field']) $this->error('请输入字段名称');
        if (!$data['desc']) $this->error('请输入配置简介');
        if ($data['sort'] < 0) {
            $data['sort'] = 0;
        }
        $config = $this->service->getOne(['name_field' => $data['name_field']]);
        if ($config) {
        	$this->error('该字段名称已存在！请重新输入');
        }
        if( $this->service->save($data)){
       	 	$this->success('添加配置成功!');
        }
       	$this->error('添加配置失败!');
    }

    /**
     * 编辑配置项
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
           	['config_name', ''],
            ['type', ''],
            ['input_type', 'input'],
            ['config_tab_id', 0],
            ['parameter', ''],
            ['upload_type', 1],
            ['required', ''],
            ['value', ''],
            ['name_field', ''],
            ['desc', ''],
            ['sort', 0],
            ['status', 0]
        ]);
        if (!$data['config_name']) $this->error('请输入配置名称');
        if (!$data['name_field']) $this->error('请输入字段名称');
        if (!$data['desc']) $this->error('请输入配置简介');
        if ($data['sort'] < 0) {
            $data['sort'] = 0;
        }
        $config = $this->service->getOne(['name_field' => $data['name_field']]);
        if ($config && $config['id'] != $id) {
        	$this->error('该字段名称已存在！请重新输入');
        }

        if($this->service->update($id, $data)){
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }

    /**
     * 删除配置项
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->delete((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 修改配置项状态
     * @method(PUT)
     */
    public function status()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->update($id, ['status' => $this->request->param('status')])) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

}